﻿using System;
using System.Net;
using Cdp;

namespace CdpServer
{
    class Program
    {
        static void Main(string[] args)
        {
            bool cdp = true;
            string address = "127.0.0.1";
            int port = 6000;
            if (args.Length > 0)
            {
                for(int i = 0; i < args.Length; i++)
                {
                    try
                    {
                        switch (args[i])
                        {
                            case "-t":
                                if (args[i + 1].Equals("udp"))
                                {
                                    cdp = false;
                                }
                                else
                                    cdp = true;
                                i++;
                                break;
                            case "-a":
                                address = args[i + 1];
                                i++;
                                break;
                            case "-p":
                                port = int.Parse(args[i + 1]);
                                i++;
                                break;
                            case "-d":
                                Logger.EnableVerbose = true;
                                break;
                            default:
                                Usage();
                                Environment.Exit(-1);
                                break;
                        }
                    }catch(Exception ex)
                    {
                        Logger.Error("error, {0}", ex.Message);
                        Usage();
                        Environment.Exit(-1);
                    }
                }
            }

            if (cdp)
            {
                CdpListener server = new CdpListener(1024);
                server.MTU = 1500;
                server.OnAccepeted += Server_OnAccepeted;
                server.OnReceived += Server_OnReceived;
                server.OnDisconnected += Server_OnDisconnected;
                server.Listen(new IPEndPoint(IPAddress.Parse(address), port));
                Logger.Information("cdp server start, listening at {0}:{1}", address, port);                
            }
            else
            {
                AsyncUdpClient server = new AsyncUdpClient(102400);
                server.OnReceived += UdpServer_OnReceived1;
                server.Listen(new IPEndPoint(IPAddress.Parse(address), port));
                Logger.Information("udp server start, listening at {0}:{1}", address, port);
            }
            Console.ReadLine();
        }

        private static void UdpServer_OnReceived1(object sender, CdpDatagram data)
        {
            Logger.Verbose("receive {0} bytes from {1}", data.BytesTransferred, data.RemoteEndPoint);
        }

        private static void Server_OnDisconnected(object sender, CdpDatagram data)
        {
            Logger.Information("cdp client {0} disconnected", data.RemoteEndPoint);
        }

        private static void Server_OnReceived(object sender, CdpDatagram data)
        {
            Logger.Verbose("receive {0} bytes from {1}", data.BytesTransferred, data.RemoteEndPoint);
        }

        private static void Server_OnAccepeted(object sender, CdpDatagram data)
        {
            Logger.Information("cdp client {0} accepeted", data.RemoteEndPoint);
        }

        private static void Usage()
        {
            Console.WriteLine("Usage:");
            Console.WriteLine("    CdpServer [-t cdp|udp] [-a address] [-p port] [-d]");
            Console.WriteLine("Option:");
            Console.WriteLine("\t-t Server type using cdp or udp");
            Console.WriteLine("\t-a Server listen address");
            Console.WriteLine("\t-p Server listen port");
            Console.WriteLine("\t-t Enable detail log");            
        }
    }
}